﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using HZ.Supervise.Model.Enum;
using System.Collections.Generic;

namespace HZ.Supervise.Web.Admin
{
    public partial class PrivilegesRole :BasePage
    {
        IList<UserRule> userRule = new List<UserRule>();
        IList<User> userList = new List<User>();
        IBLL_User iu = HZ.Supervise.BLLFactory.BLL_User();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!CurrentAccount.GetUser().IsAdmin || !CurrentAccount.GetUser().Org.OrgID.Equals("AD002"))
            {
                PageError("您不是管理员，不能分配特殊权限！", "");
                return;
            }

            if (!IsPostBack)
            {
                bindUser();
            }
        }
        /// <summary>
        /// 定义绑定用户的方法
        /// </summary>
        private void bindUser()
        {
            //获取查询条件，执行查询

                AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
                int totalRecord = 0;
                string where = "";
            
                    where += "and UserID in('AD147','AD148','AD149','AD004','AD003','AD005','AD006','AD007','AD011','AD012','AD009','AD013','AD015','AD010','AD014','AD008','AD016')";
                    userList = iu.GetUserDetailByWhere(where, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalRecord);
                    this.gvwChildUserList.DataSource = userList;
                    string where1 = "";
                    for (int i = 0; i < userList.Count; i++)
                    {
                        where1 += "'" + userList[i].UserID + "',";
                    }
                    userRule = iu.GetUserRuleDetailBywhere(where1.TrimEnd(','));
                    AspNetPager1.RecordCount = totalRecord;
                    this.gvwChildUserList.DataBind();
                    //for (int i = 0; i < userRule.Count; i++)
                    //{
                    //    for (int j = 0; j < userList.Count; j++)
                    //    {
                    //        if (userRule[i].UserID.Equals(userList[j].UserID))
                    //        {
                    //            ((CheckBox)gvwChildUserList.Rows[j].Cells[3].FindControl("chkChildUser")).Checked = true;
                    //        }
                    //    }
                    //}
                
               
            
        }


        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            bindUser();
        }

        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            bindUser();
        }

        protected void gvwUser_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {

        }

        protected void gvwChildUserList_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void gvwChildUserList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
          
        }

        protected void gvwChildUserList_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void gvwChildUserList_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

        }

        protected void btnBatResetPws_Click(object sender, EventArgs e)
        {
            IList<UserRule> userRouleListAdd = new List<UserRule>();
            IList<UserRule> userRouleListDelete = new List<UserRule>();
            for (int i = 0; i < gvwChildUserList.Rows.Count; i++)
            {
                UserRule user = new UserRule();
                CheckBox check = (CheckBox)gvwChildUserList.Rows[i].Cells[3].FindControl("chkChildUser");
                if (check.Checked)
                {
                    user.UserID = gvwChildUserList.Rows[i].Cells[1].Text.ToString();
                    user.IsEffective =true;
                    userRouleListAdd.Add(user);
                }
                else 
                {
                    user.UserID = gvwChildUserList.Rows[i].Cells[1].Text.ToString();
                    user.IsEffective = true;
                    userRouleListDelete.Add(user);
                }
              
            }
            int res = iu.UpdateUserRule(userRouleListAdd, userRouleListDelete);
            if (res > 0)
            {
                HZ.Web.WebUtil.Alert("修改成功！");
                bindUser();
            }
            else if (res==0)
            {
                 HZ.Web.WebUtil.Alert("没有数据修改！");
            }

            else
            {

                HZ.Web.WebUtil.Alert("修改失败！");
            }
        }
      
    }
}
